from bs4 import BeautifulSoup
# 安装BeautifulSoup
# pip install bs4
html = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""

soup = BeautifulSoup(html, 'lxml')
# 使用prettify方法，把解析的字符以标准缩进格式输出
print(soup.prettify())
# 选择元素
print(soup.title)
print(soup.title.string)
print(soup.p)

#提取信息,获取节点的名称
print(soup.title.name)

#提取信息，获取属性
print(soup.p.attrs)

#获取内容
print(soup.p.string)


#嵌套选择
print(soup.head.title)